package com.xiaotu.spider.baidu.pipeline;

import com.xiaotu.common.model.NetTVModel;
import com.xiaotu.common.model.TVRoleModel;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.File;
import java.util.List;

/**
 * 百度百科角色介绍存储类
 *
 * @auther GaoHJ
 * @create 2017/10/23
 */
@Component("BaiduBaikeRolePipeline")
public class BaiduBaikeRolePipeline extends APipeline
{
    private static final String[] TV_FIELD_ARR = new String[]{"tvId", "tvName", "type",
            "director", "actor", "url", "timestamp"};
    private static final String[] ROLE_FIELD_ARR = new String[]{"tvId", "roleName", "actor",
            "roleDesc"};

    private static final String[] ROLE_CONVERTER_ARR = new String[]{AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT};
    private static final String[] TV_CONVERTER_ARR = new String[]{AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRINGARRAY_CONVERT, AConverter
            .STRINGARRAY_CONVERT, AConverter.STRINGARRAY_CONVERT, AConverter.STRING_CONVERT,
            AConverter.TIMESTAMP_CONVERT};

    private static final boolean[] TV_THROW_ARR = new boolean[]{true, true, true, true, true,
            true, true};
    private static final boolean[] ROLE_THROW_ARR = new boolean[]{true, true, true, true};

    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
        String filePath = resultItems.get("filePath");

        NetTVModel tv = resultItems.get(NetTVModel.class.getName());
        if (tv != null)
            FileUtil.writeFile(filePath + File.separator + "tv.csv", StringUtil.list2CSVLine
                    (ConverterFactory.convert(JSONObject.fromObject(tv), TV_FIELD_ARR,
                            TV_CONVERTER_ARR, TV_THROW_ARR), SepratorUtil.SEP_PIPE) +
                    SepratorUtil.LINE_SEPRATOR);

        List<TVRoleModel> roleList = resultItems.get(TVRoleModel.class.getName());
        if (roleList != null && !roleList.isEmpty())
            roleList.forEach(role -> FileUtil.writeFile(filePath + File.separator + "role.csv",
                    StringUtil.list2CSVLine(ConverterFactory.convert(JSONObject.fromObject(role),
                            ROLE_FIELD_ARR, ROLE_CONVERTER_ARR, ROLE_THROW_ARR), SepratorUtil
                            .SEP_PIPE) + SepratorUtil.LINE_SEPRATOR));

    }
}
